package com.wxzz.elearing.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.course.dto.CurriculumCategoryDto;
import com.wxzz.elearing.course.entity.CurriculumCategory;
import com.wxzz.elearing.course.mapper.CurriculumCategoryMapper;
import com.wxzz.elearing.course.service.ICurriculumCategoryService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author
 * @since 2021-03-10
 */
@Service
public class CurriculumCategoryServiceImpl extends ServiceImpl<CurriculumCategoryMapper, CurriculumCategory> implements ICurriculumCategoryService {

    @Autowired
    private CurriculumCategoryMapper curriculumCategoryMapper;

    @Override
    public ReturnVO<JgPageVo<List<CurriculumCategory>>> listCurriculumCategory(Long enterpriseId, CurriculumCategoryDto curriculumCategoryDto) {
        PageHelper.startPage(curriculumCategoryDto.getCurPage(), curriculumCategoryDto.getMaxPage());
        QueryWrapper<CurriculumCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id", enterpriseId);
        queryWrapper.eq("pid", 0);
        if (StringUtils.isNotBlank(curriculumCategoryDto.getCategoryName())) {
            queryWrapper.like("category_name", curriculumCategoryDto.getCategoryName() + "%");
        }
        //queryWrapper.orderByDesc("create_time");
        //查询一级分类
        List<CurriculumCategory> topCategory = curriculumCategoryMapper.selectList(queryWrapper);
        for (CurriculumCategory curriculumCategory : topCategory) {
            //查询二级分类
            List<CurriculumCategory> secondCategories = curriculumCategoryMapper.listCurriculumCategoryByPid(curriculumCategory.getId());
            for (CurriculumCategory secondCategory : secondCategories) {
                //平台课程根据平台课程分类字段查询
                if (curriculumCategory.getEnterpriseId() == 0) {
                    secondCategory.setCurriculumNum(curriculumCategoryMapper.countByPlatformCurriculumCategoryId(secondCategory.getId()));
                }
                //企业课程根据企业课程分类字段查询
                if (curriculumCategory.getEnterpriseId() != 0) {
                    secondCategory.setCurriculumNum(curriculumCategoryMapper.countByEnterpriseCurriculumCategoryId(secondCategory.getId()));
                }
                //将二级分类下的课程总数加到一级分类上
                curriculumCategory.setCurriculumNum(curriculumCategory.getCurriculumNum() + secondCategory.getCurriculumNum());
            }
            curriculumCategory.setChildren(secondCategories);
        }
        return new ReturnVO("分类列表查询成功", new JgPageVo(new PageInfo<>(topCategory)));
    }

    @Override
    public ReturnVO<List<CurriculumCategory>> listCurriculumCategory(Long enterpriseId) {
        List<CurriculumCategory> curriculumCategories = curriculumCategoryMapper.listCurriculumCategoryByEnterpriseIdAndPid(enterpriseId, 0L);
        for (CurriculumCategory curriculumCategory : curriculumCategories) {
            curriculumCategory.setChildren(curriculumCategoryMapper.listCurriculumCategoryByEnterpriseIdAndPid(enterpriseId, curriculumCategory.getId()));
        }
        return new ReturnVO("列出课程分类搜索", curriculumCategories);
    }

    @Override
    public ReturnVO saveCurriculumCategory(CurriculumCategory curriculumCategory) {
        curriculumCategoryMapper.insert(curriculumCategory);
        return new ReturnVO("分类新增成功", curriculumCategory.getId());
    }

    @Override
    public ReturnVO updateCurriculumCategory(CurriculumCategory curriculumCategory) {
        curriculumCategoryMapper.updateById(curriculumCategory);
        return new ReturnVO("分类修改成功", curriculumCategory.getId());
    }

    @Override
    public ReturnVO deleteCurriculumCategory(Long curriculumCategoryId) {
        curriculumCategoryMapper.deleteById(curriculumCategoryId);
        return new ReturnVO("删除课程分类", curriculumCategoryId);
    }
}
